﻿using lwzlz.Application;
using lwzlz.Application.System;
using lwzlz.Common.VerifyCode;
using lwzlz.Web.Areas.SysAdmin.Models;
using System;
using System.Web.Mvc;

namespace lwzlz.Web.Areas.SysAdmin.Controllers
{
    public class LoginController : BaseController
    {
        #region 首页
        public ActionResult Index()
        {

            return View();
        }
        #endregion

        #region 登录
        public JsonResult Login(string username, string password, string verifyCode)
        {
            ResponseModel response = new ResponseModel();
            LoginService service = new LoginService();
            try
            {
                Result result = service.Login(username, password, verifyCode);
                if (result.Success)
                {
                    response.Success = true;
                    response.Data = Url.RouteUrl(new { area = "SysAdmin", controller = "Home", action = "Index" });
                    response.Message = "登录成功";
                }
                else
                {
                    response.Success = false;
                    response.Message = result.Message;
                }
            }
            catch (Exception ex)
            {
                response.Success = false;
                response.Message = ex.Message;
            }
            return Json(response);
        }
        #endregion

        #region 生成验证码
        public FileResult CreateVerifyCode()
        {
            var verifyCode = new VerifyCode { Chaos = true };
            var checkCode = verifyCode.CreateVerifyCode();
            Session[verifyCode.VERIFYCODE_SESSIONKEY] = checkCode;//用于客户端校验码比较
            Response.Expires = -1;
            return File(verifyCode.CreateImageOnPage(checkCode), "image/Gif");
        }
        #endregion

        #region 注销
        public ActionResult Logout()
        {
            LoginService service = new LoginService();
            try
            {
                service.Logout();
            }
            catch (Exception)
            {

            }

            return RedirectToAction("Index", "Login");
        }
        #endregion

        #region 登录异常页面
        public ActionResult LoginError()
        {
            return View();
        }
        #endregion
    }
}